# coding: utf-8

# Copyright (c) 2020 CSTCHC Technologies Co., Ltd.
# oec-hardware is licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.

"""card test"""
import os
import subprocess
import time

from cstchccompatible.command import Command, CertCommandError
from cstchccompatible.test import Test

class Card_test(Test):
    """
       modules test
    """
    def __init__(self):
        Test.__init__(self)

    def current_card(self):

        print("INFO: the manufacturer's information and equipment model", "\n")
        print
        pci = subprocess.check_output("lspci |grep NVIDIA |awk '{print $1}'", shell=True, universal_newlines=True)
        if pci:
            pcis = pci.split()
            for pci_id in pcis:
                Command('lspci -s %s -v' % pci_id).echo()
        else:
            print("WARNING: the manufacturer's information is not exist ###")
        print

        print('INFO: Drive information:', "\n")

        Command('modinfo nvidia|head -n 13').echo()
        print     

    def pressure(self):

        print("INFO：Using gpu_burn carries out pressure test, and nvidia-smi " +
        "is used to monitor GPU temperature and utilization rate\n")
        
        os.system('cd /usr/bin && nohup gpu_burn 20 &')
        while not subprocess.call("ps -ef |grep 'gpu_burn' | grep -v grep", shell=True):
            Command('nvidia-smi').echo()
            time.sleep(1)

        Command('nvidia-smi').echo()

    def test(self):
        try:
            self.current_card()
            self.pressure()
            return True
        except Exception as concrete_error:
            print(concrete_error)
            return False
